package com.example.queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.locks.ReentrantLock;

/**
 * description: SynchronousQueueDemo <br>
 * date: 2021\3\23 10:46 <br>
 *
 * @author: ZhangYixin <br>
 * version: 1.0 <br>
 */
public class SynchronousQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> queue = new SynchronousQueue<String>();

        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"\t 做好了第一个 put 1");
                queue.put("1");
                System.out.println(Thread.currentThread().getName()+"\t 做好了第一个 put 2");
                queue.put("2");
                System.out.println(Thread.currentThread().getName()+"\t 做好了第一个 put 3");
                queue.put("3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"师傅").start();

        new Thread(()->{
            try {
                System.out.println(Thread.currentThread().getName()+"\t 买走了一个 take "+queue.take());
                System.out.println(Thread.currentThread().getName()+"\t 买走了一个 take "+queue.take());
                System.out.println(Thread.currentThread().getName()+"\t 买走了一个 take "+queue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"顾客").start();

        ReentrantLock reentrantLock = new ReentrantLock();
        reentrantLock.notify();
    }
}
